/// <reference name="MicrosoftAjax.debug.js" />
/// <reference path="../../Resources/js/jquery/jquery.debug.js" />
/// <reference path="../Navigation/ItemsNavigator.debug.js />


// CombinedDescriptor object

BM.CombinedDescriptor = function(item, data) {
    BM.CombinedDescriptor.initializeBase(this, [item, data]);
}

BM.CombinedDescriptor.prototype = {
    load: function() {
        BM.CombinedDescriptor.callBaseMethod(this, 'load');
        this.item.descriptor = this;
        this.loadChunks();
        this.getItem().getMForm().add_loadComplete(Function.createDelegate(this, this.raiseDescriptionChanged));
    },

    loadChunks: function() {
        this.chunks = new Array();
        for (var i = 0; i < this.data.chunks.length; i++) {
            var className = this.data.chunks[i].ClassName;
            this.chunks.push(new BM.DescriptionChunk[className](this, this.data.chunks[i]));
        }
    },

    getDescription: function() {
        var builder = new Sys.StringBuilder();
        for (var i = 0; i < this.getChunks().length; i++) {
            builder.append(this.getChunks()[i].toString());
        }
        return builder.toString();
    },

    getChunks: function() {
        return this.chunks;
    }
}

BM.CombinedDescriptor.registerClass("BM.CombinedDescriptor", BM.Descriptor);

// Chunk object

BM.DescriptionChunk = function(descriptor, data) {
    this.descriptor = descriptor;
    this.data = data;
}

BM.DescriptionChunk.registerClass("BM.DescriptionChunk");

//TextChunk object

BM.TextChunk = function(descriptor, data) {
    BM.TextChunk.initializeBase(this, [descriptor, data]);
}

BM.TextChunk.prototype = {
    toString: function() {
        var output = this.data.Value;
        if (output == null || output == '') return ' ';
        return output;
    }
}

BM.TextChunk.registerClass("BM.TextChunk", BM.DescriptionChunk);
BM.DescriptionChunk['BM.TextDescription'] = BM.TextChunk;

//ValueChunk object

BM.ValueChunk = function(descriptor, data) {
    BM.ValueChunk.initializeBase(this, [descriptor, data]);
    this.getDescriptorItem().addChanged(Function.createDelegate(this.descriptor, this.descriptor.raiseDescriptionChanged));
}

BM.ValueChunk.prototype = {
    getItem: function() {
        return this.descriptor.item;
    },

    toString: function() {
        if (this.data.CustomDescriptionMethod != null) {
            try {
                var customMethod = eval(this.data.CustomDescriptionMethod)
                return customMethod(this.getDescriptorItem(), { valueChunk: this });
            }
            catch (e) {
            }
        }
        return this.getDescriptorItem().getValue();
    },

    getDescriptorItem: function() {
        return BM.ItemsNavigator.selectSingle(this.getItem(), this.getPath());
    },

    getPath: function() {
        return this.data.Path;
    }

}

BM.ValueChunk.registerClass("BM.ValueChunk", BM.DescriptionChunk);
BM.DescriptionChunk['BM.ValueDescription'] = BM.ValueChunk;

